use rusty_engine::game::Engine;
use rusty_engine::prelude::{TEXT_DEFAULT_FONT_SIZE, TEXT_DEFAULT_LAYER};
use rusty_engine::RIGHT;
use crate::game_state::{GameScene, GameState};
use crate::scenes::button::{click_check, hover_check, in_button};
use crate::scenes::switch_scene;
use crate::UI_BOTTOM_LAYER;

pub fn logic(engine: &mut Engine, game_state: &mut GameState) {
    
    // 初始化逻辑
    if game_state.game_over_init == false {
        init_game_over(engine,game_state);
        game_state.game_over_init = true;
    }
    
    // 点击检测
    if click_check(engine, game_state,"restart_button") {
        // 单人模式 
        if game_state.last_scene == GameScene::SingleGame {
            switch_scene(game_state, GameScene::SingleGame);
        } else {
            switch_scene(game_state, GameScene::DoubleGame);
        }
    }
    click_check(engine, game_state,"menu_button");

    // 悬停检测
    hover_check(engine, "restart_button");
    hover_check(engine, "menu_button");
    
    // 非悬停检测
    if in_button(engine,"restart_button") == false {
        if let Some(button) = engine.sprites.get_mut("restart_button") {
            button.scale = 0.75; // 按钮大小恢复
        }
    }
    if in_button(engine,"menu_button") == false {
        if let Some(button) = engine.sprites.get_mut("menu_button") {
            button.scale = 0.75; // 按钮大小恢复
        }
    }
    
    // 如果没有在播放音乐，播放音乐
    if engine.audio_manager.music_playing() == false {
        engine.audio_manager.play_music("music/music/song_rainonbrick.ogg", 0.8);
    }
}

fn init_game_over(engine: &mut Engine, game_state: &mut GameState) {
    // 将其他界面初始化设置为false，避免发生错误
    game_state.game_single_init = false;
    game_state.main_menu_init = false;
    game_state.game_double_init = false;

    // 停止播放音乐
    engine.audio_manager.stop_music();

    // 播放音乐
    engine.audio_manager.play_music("music/song_rainonbrick.ogg", 0.8);

    // 清除文本
    engine.texts.clear();
    
    // 添加重新开始按钮
    let start_button = engine.add_sprite("restart_button", "sprite/button/button_blue.png");
    start_button.rotation = RIGHT;
    start_button.translation.y = -95.0;
    start_button.scale = 0.75;
    start_button.layer = UI_BOTTOM_LAYER;
    start_button.collider_dirty = true;
    // 重新开始按钮文本
    let start_game_text = engine.add_text("restart_game_text","RESTART");
    start_game_text.translation.y = - 95.0;
    start_game_text.layer = TEXT_DEFAULT_LAYER;
    start_game_text.font_size = TEXT_DEFAULT_FONT_SIZE;

    // 添加返回主菜单按钮
    let menu_button = engine.add_sprite("menu_button", "sprite/button/button_blue.png");
    menu_button.rotation = RIGHT;
    menu_button.translation.y = -265.0;
    menu_button.scale = 0.75;
    menu_button.layer = UI_BOTTOM_LAYER;
    menu_button.collider_dirty = true;
    
    // 返回主菜单按钮文本
    let menu_text = engine.add_text("menu_text","RETURN");
    menu_text.translation.y = - 265.0;
    menu_text.layer = TEXT_DEFAULT_LAYER;
    menu_text.font_size = TEXT_DEFAULT_FONT_SIZE;

    // 标题文本
    let title_text = engine.add_text("game_over_text","GAME OVER");
    title_text.translation.y = 180.0;
    title_text.layer = TEXT_DEFAULT_LAYER;
    title_text.font_size = TEXT_DEFAULT_FONT_SIZE * 5.0;

    // 战绩
    // 单人模式
    if game_state.last_scene == GameScene::SingleGame {
        // 得分
        let score_text = engine.add_text("score_text",format!("最终得分: {}", game_state.score[0] as i32));
        score_text.translation.y = 80.0;
        score_text.translation.x = -20.0;
        score_text.font = "font/HYPixel11pxU-2.ttf".to_string();
        score_text.layer = TEXT_DEFAULT_LAYER;
        score_text.font_size = TEXT_DEFAULT_FONT_SIZE * 0.8;
        // 距离
        let distance_text = engine.add_text("distance_text",format!("最终距离: {}", game_state.distance[0] as i32));
        distance_text.translation.y = 50.0;
        distance_text.translation.x = -20.0;
        distance_text.font = "font/HYPixel11pxU-2.ttf".to_string();
        distance_text.layer = TEXT_DEFAULT_LAYER;
        distance_text.font_size = TEXT_DEFAULT_FONT_SIZE * 0.8;
        // 金币数
        let coin_text = engine.add_text("coin_text",format!("最终金币: {}", game_state.coins[0] as i32));
        coin_text.translation.y = 20.0;
        coin_text.translation.x = -20.0;
        coin_text.font = "font/HYPixel11pxU-2.ttf".to_string();
        coin_text.layer = TEXT_DEFAULT_LAYER;
        coin_text.font_size = TEXT_DEFAULT_FONT_SIZE * 0.8;
    } else {
        // 玩家1得分
        let score_text = engine.add_text("score_1_text",format!("玩家1得分: {}", game_state.score[0] as i32));
        score_text.translation.y = 80.0;
        score_text.translation.x = -90.0;
        score_text.font = "font/HYPixel11pxU-2.ttf".to_string();
        score_text.layer = TEXT_DEFAULT_LAYER;
        score_text.font_size = TEXT_DEFAULT_FONT_SIZE * 0.8;
        // 玩家2得分
        let score_text = engine.add_text("score_2_text",format!("玩家2得分: {}", game_state.score[1] as i32));
        score_text.translation.y = 80.0;
        score_text.translation.x = 90.0;
        score_text.font = "font/HYPixel11pxU-2.ttf".to_string();
        score_text.layer = TEXT_DEFAULT_LAYER;
        score_text.font_size = TEXT_DEFAULT_FONT_SIZE * 0.8;
        // 玩家1距离
        let distance_text = engine.add_text("distance_1_text",format!("玩家1距离: {}", game_state.distance[0] as i32));
        distance_text.translation.y = 50.0;
        distance_text.translation.x = -90.0;
        distance_text.font = "font/HYPixel11pxU-2.ttf".to_string();
        distance_text.layer = TEXT_DEFAULT_LAYER;
        distance_text.font_size = TEXT_DEFAULT_FONT_SIZE * 0.8;
        // 玩家2距离
        let distance_text = engine.add_text("distance_2_text",format!("玩家2距离: {}", game_state.distance[1] as i32));
        distance_text.translation.y = 50.0;
        distance_text.translation.x = 90.0;
        distance_text.font = "font/HYPixel11pxU-2.ttf".to_string();
        distance_text.layer = TEXT_DEFAULT_LAYER;
        distance_text.font_size = TEXT_DEFAULT_FONT_SIZE * 0.8;
        // 玩家1金币数
        let coin_text = engine.add_text("coin_1_text",format!("玩家1金币: {}", game_state.coins[0] as i32));
        coin_text.translation.y = 20.0;
        coin_text.translation.x = -90.0;
        coin_text.font = "font/HYPixel11pxU-2.ttf".to_string();
        coin_text.layer = TEXT_DEFAULT_LAYER;
        coin_text.font_size = TEXT_DEFAULT_FONT_SIZE * 0.8;
        // 玩家2金币数
        let coin_text = engine.add_text("coin_2_text",format!("玩家2金币: {}", game_state.coins[1] as i32));
        coin_text.translation.y = 20.0;
        coin_text.translation.x = 90.0;
        coin_text.font = "font/HYPixel11pxU-2.ttf".to_string();
        coin_text.layer = TEXT_DEFAULT_LAYER;
        coin_text.font_size = TEXT_DEFAULT_FONT_SIZE * 0.8;
    }
}

